package com.example.Service.Impl;

import com.example.Dao.ArticleMapper;
import com.example.Service.ArticleService;
import com.example.pojo.Article;
import com.example.pojo.PageBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;


    //2.添加文章
    @Override
    public void addArticle(Article article,Integer createId) {

        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateUser(createId);

        articleMapper.add(article);
    }

    //3.删除文章
    @Override
    public void delete(Integer id) {
        articleMapper.deleteArticle(id);
    }


    //条件查询文章列表
    @Override
    public PageBean page(Integer page, Integer pageSize, Integer categortId, String state, Integer createId) {

        //1.查询总条数
        Long totalNum = articleMapper.total();

        //2.获取页码，计算起始索引
        Integer startIndex = (page - 1) * pageSize;
        List<Article> articles = articleMapper.page(startIndex,pageSize,categortId,state,createId);

        //3.分装pageBean
        PageBean pb = new PageBean(totalNum,articles);

        return pb;
    }
}
